﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using LGE;

namespace MagicFight
{
    public class ArcaneBomb : Magic
    {
        Projectile projectile = null;
        int damage = Def.ArcaneBombDef.Damage;
        int radius = Def.ArcaneBombDef.Radius;
        int timer = Def.ArcaneBombDef.Timer;

        public ArcaneBomb(Character caster)
            : base(caster, Def.ArcaneBombDef.N)
        {
        }

        public override void Effect(GameTime gameTime)
        {            
            if (projectile == null)
            {
                projectile = new Projectile(caster, Engine.Load<Texture2D>("images\\projectile"));
                projectile.canCollideWithCaster = true;
                projectile.velocity = 0;
                projectile.position += projectile.direction * 50;
                projectile.maxLifeTime = timer;
                ps.gameObjects.Add(projectile);
            }
            if (projectile.collided) projectile.alive = false;
            if (!projectile.alive) // entra aqui se morreu por tempo ou por colisão 
                Explode();
        }

        private void Explode()
        {
            alive = false;
            ps.effectsManager.Add(new GreenGlow(projectile.position, 100));
            foreach (GameObject obj in ps.gameObjects)
            {
                if (obj == null) continue;
                if (!obj.alive) continue;
                if ((Vector2.Distance(projectile.position, obj.position)) < radius)
                {
                    if (obj is Character)
                    {
                        Character charTarget = obj as Character;
                        int d = charTarget.TakeDamage(damage, caster);
                        if (d > 0) ps.effectsManager.Add(new FloatingText(charTarget.position, d.ToString(), Color.Red));
                    }
                    else if (obj is Projectile)
                    {
                        (obj as Projectile).collided = true;
                    }
                }
            }
        }
    }
}
